////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008 Duykham.
// All Rights Reserved.
// Author: Duykham (duykham@gmail.com)
// Modified: 20081030
// TODO: Load data from an XML file
//
////////////////////////////////////////////////////////////////////////////////
package dk.agrasys
{
	import flash.events.*;
	
	import mx.collections.ArrayCollection;
	import mx.collections.IViewCursor;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	import mx.utils.ArrayUtil;

	/**
	 * Load data from an XML file 
	 * @author Duykham
	 * 
	 */	
	public class LoadData
	{	
		private var service:HTTPService;

		/**
		 * Array contains loaded data 
		 */		
        [Bindable]
		public var datas:ArrayCollection;
		
		/**
		 * Constructor 
		 * @param url
		 * 
		 */		
		public function LoadData(url:String)
		{
			service = new HTTPService();
			service.url = url;
			service.addEventListener(ResultEvent.RESULT, resultHandler);
			service.send();
		}
		
		/**
		 * Handle results when data is loaded 
		 * @param event
		 * 
		 */		
		private function resultHandler(event:ResultEvent):void
		{
			if (event.result.ss.s is ArrayCollection)
			{
			    var result:ArrayCollection = event.result.ss.s is ArrayCollection
			        ? event.result.ss.s as ArrayCollection
			        : new ArrayCollection(ArrayUtil.toArray(event.result.ss.s));    
			    var temp:ArrayCollection = new ArrayCollection();
			    var cursor:IViewCursor = result.createCursor();
			    while (!cursor.afterLast)
			    {
			    	var s:Object = new Sensor(cursor.current);
			        temp.addItem({nhietdo:s.nhietdo, doam:s.doam, label:s.time});
			        cursor.moveNext();
			    }
			    datas = temp;
		 	}
		}
	}
	
}